package com.wcj.springboot.controller;


import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.wcj.springboot.entity.Files;
import com.wcj.springboot.entity.User;
import com.wcj.springboot.mapper.FileMapper;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.wcj.springboot.service.ICompService;
import com.wcj.springboot.entity.Comp;
import com.wcj.springboot.common.Result;

import org.springframework.web.bind.annotation.*;


import org.springframework.web.bind.annotation.RestController;

import static sun.misc.Version.println;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wcj
 * @since 2022-09-08
 */
@RestController
@RequestMapping("/comp")
public class CompController {



    @Resource
    private  ICompService compService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public static final String COMPS_KEY = "COMP_ALL";

    @PostMapping
    public Result saveOrUpdate(@RequestBody Comp comp){
        //        flushRedis(COMPS_KEY);
        return  Result.success(compService.saveOrUpdate(comp));
    }

    @PostMapping("/save/batch")
    public Result saveOrUpdateByIds(@RequestBody List<Comp> comps){
//        flushRedis(COMPS_KEY);
        return  Result.success(compService.saveOrUpdateBatch(comps));
    }

    @DeleteMapping("/{id}")
    public Result removeById(@PathVariable Integer id){
        //        flushRedis(COMPS_KEY);
        return Result.success(compService.removeById(id));
    }


    @PostMapping("/del/batch")
    public Result removeBatchByIds(@RequestBody List<Integer> ids){
        //        flushRedis(COMPS_KEY);
        return Result.success(compService.removeBatchByIds(ids));
    }


    @GetMapping
    public Result getList(){
        return Result.success(compService.list());
    }

    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        return Result.success(compService.getById(id));
    }

    @GetMapping("/current/{uid}/{page}")
    public Result getByUIDPage(@PathVariable String uid,@PathVariable String page){
        List<Comp> comps;
//        String jsonStr = stringRedisTemplate.opsForValue().get(COMPS_KEY);
//        if(StrUtil.isBlank(jsonStr)){
//            QueryWrapper<Comp> queryWrapper = new QueryWrapper<>();
//            queryWrapper.eq("uid",uid);
//            queryWrapper.eq("page",page);
//            comps =  compService.list(queryWrapper);
//            stringRedisTemplate.opsForValue().set(COMPS_KEY, JSONUtil.toJsonStr(comps));
//        }else{
//            comps = JSONUtil.toBean(jsonStr, new TypeReference<List<Comp>>() {
//            },true);
//        }

        QueryWrapper<Comp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        queryWrapper.eq("page",page);
        comps =  compService.list(queryWrapper);
        return Result.success(comps);
    }

    @GetMapping("/uid/{uid}")
    public Result getByUID(@PathVariable String uid){
        QueryWrapper<Comp> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid",uid);
        return Result.success(compService.list(queryWrapper));
    }


    @GetMapping("/page")
    public  Result getPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize,
                                @RequestParam(defaultValue = "") String id){
            IPage<Comp> page = new Page<>(pageNum, pageSize);
            QueryWrapper<Comp> queryWrapper = new QueryWrapper<>();
            if(!"".equals(id)){
            queryWrapper.like("id",id);
            }
            queryWrapper.orderByDesc("id");
            return Result.success(compService.page(page,queryWrapper));
   }


   private  void flushRedis(String key){
        stringRedisTemplate.delete(key);
   }
}

